package command_impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import app.OrderApp;
import app.OrderDao;
import command.UserCommand;
import pojo.Order;
import pojo.OrderItem;
import pojo.OrderStatus;
import pojo.Payment;
import pojo.PaymentStatus;
import pojo.Product;
import pojo.User;
import util.OrderConsole;

public class BuyCommand extends UserCommand{

	public BuyCommand(User user) {
		super(user);
		// TODO Auto-generated constructor stub
	}

	@Override	
	public void execute() {
		
		new ListCommand().execute();
		OrderDao dao = new OrderDao();
		List<Product> products = dao.getAllProducts();
		Set<Integer> idSet = new HashSet<>();
		for(Product product : products){
			idSet.add(product.getId());
		}
		
		double money = 0;
		//新建订单
		Order order = new Order();
		OrderItem orderItem = null;
		Payment payment = new Payment();
		
		//订单创建时间
		SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
		order.setCreateDatetime(sdf.format(new Date()));
		payment.setPaymentStatus(PaymentStatus.values()[1]);
		order.setStatus(OrderStatus.values()[1]);
		order.setPayment(payment);
		order.setClient(OrderApp.getInstance().getUser().getClient());
		
		
		while(true){
			int productId = OrderConsole.askUserInputInt("\n请输入要购买的商品的ID");
			
			if(!idSet.contains(productId)){
				System.out.println("不存在该商品!");
				continue;
			}
			Product product = dao.getProduct(productId);
			int amount = OrderConsole.askUserInputInt("购买数量");			
			
			int index = itemExist(order, product);
			
			if(index != -1){
				orderItem = order.getOrderItems().get(index);
				orderItem.setAmount(orderItem.getAmount()+amount);
				orderItem.setMoney(orderItem.getMoney()+amount*product.getPrice());
				order.getOrderItems().remove(index);
				order.getOrderItems().add(orderItem);
			}else{
				orderItem = new OrderItem();
				orderItem.setAmount(amount);
				orderItem.setProduct(product);				
				orderItem.setMoney(amount*product.getPrice());
				order.getOrderItems().add(orderItem);
			}
			money+=orderItem.getMoney();
			order.setMoney(money);
			String buy = OrderConsole.askUserInput("是否继续购买?（Y/N）");
			if("n".equalsIgnoreCase(buy)){
				break;
			}else if(! "y".equalsIgnoreCase(buy)){
				System.out.println("虽然不知道你在输入什么，但是还是当你要继续买好了");
			}
		}
		System.out.println("\n订单信息预览:");
		AllOrdersCommand.showOrder(order);
		System.out.println(">>订单总金额:"+order.getMoney()+"元");
		System.out.print("支付状态:"+order.getPayment().getPaymentStatus());
		if(order.getPayment().getPaymentStatus().equals(PaymentStatus.已付款)){
			System.out.println("  支付方式:"+order.getPayment().getPaymentMethod());				
		}else{
			System.out.println();
		}
		System.out.println("订单状态:"+order.getStatus());
		System.out.println("—————————————————————————————————————————\n");

		
		order = dao.createOrder(order);
		
		for(int i =0;i<order.getOrderItems().size();i++){
			order.getOrderItems().get(i).setId(order.getId());
		}
		
		user.getClient().getOrders().add(order);
		String goPay = OrderConsole.askUserInput("现在去支付?(Y/N)");
		if("y".equalsIgnoreCase(goPay)){
			new PayCommand(user).execute();
		}else if(! "n".equalsIgnoreCase(goPay)){
			System.out.println("就当你是要支付吧");
			new PayCommand(user).execute();
		}else {
			
			dao.updateOrder(order);
			for(OrderItem oItem : order.getOrderItems()){
				dao.updateOrderItem(oItem,order);
			}
		}
	}
	
	public int itemExist(Order order,Product product){
		OrderItem oItem = null;
		for(int i =0;i<order.getOrderItems().size();i++){
			oItem = order.getOrderItems().get(i);
			if(oItem.getProduct()==null){
				continue;
			}
			if(oItem.getProduct().getId()== product.getId()){
				return i;
			}
		}
		return -1;
	}
}
